import numpy
from numpy import linalg as la
def reduceDim(Xmatrix, exRate=0.99):
    '''
    :param Xmatrix: 解释变量矩阵：第k行为第k个样本的dim个变量
    :param dim: 变量原始维度
    :param exRate:降维后变量应该能解释exRate的降维前变量，通常为0.99，0.95,
    :return:降维后的Xmatrix', 与Xmatrix格式相同
    '''
    dim = Xmatrix[0].__len__()
    X = numpy.mat(Xmatrix)
    Xt = X.transpose()
    cov = Xt * X / dim
    matrix = numpy.mat(cov)
    U, sigma, V = la.svd(matrix)
    sigmaSum = 0
    k = dim
    print("降维前的解释变量个数k(包括截距):", k+1)
    for i in range(dim):
        sigmaSum += sigma[i]
    for i in range(1, dim + 1):
        subSum = 0
        for j in range(i):
            subSum += sigma[j]
        rate = subSum / sigmaSum
        if rate >= exRate:
            k = i
            break
    print("降维后的解释变量个数k(包括截距):", k + 1)
    answer = Xmatrix
    if k != dim:
        print("success to reduce dimensionality!")
        Ureduce = U[:, :k]
        Z = X * Ureduce
        answer = Z.tolist()
    else:
        print("fail to reduce dimensionality!")
        print("the exRate is to large")
    for i in range(answer.__len__()):
        answer[i].append(1)
    return answer

def normalize(data,start,end):
    '''
    数据归一化
    :param data:
    :return: 包含日期、日收盘价、日成交量
    '''
    small=[]
    big=[]
    all=[]
    for i in range(start,end):
        small.append(float(data[0][i]))
        big.append(float(data[0][i]))
        all.append(0)
    n=data.__len__()
    for i in data:
        for j in range(start,end):
            i[j]=float(i[j])
        for j in range(start,end):
            if i[j]<small[j-start]:
                small[j-start]=i[j]
            if i[j]>big[j-start]:
                big[j-start]=i[j]
            all[j-start]+=i[j]
    for i in range(end-start):
        all[i]=all[i]/n
        big[i]=big[i]-small[i]

    answer=[]
    for i in range(data.__len__()):
        line=[]
        for j in range(start,end):
            line.append((data[i][j]-all[j-start])/big[j-start])
        answer.append(line+data[i][end:])
    return answer
